package com.cpr.core.rabbitmq.client.routing;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;
import java.util.Random;

/**
 * @author Chen Panrui
 * created at 2022/3/15 15:14
 */
public class Producer {

    private static final String[] LOG_LEVEL = {"ERROR","WARN","FATAL"};
    private static final Random RANDOM = new Random();

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        // 指定协议 用户名+密码+host+端口号+虚拟主机
        factory.setUri("amqp://admin:123456@106.13.89.7:5672/%2f");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        // 声明交换器，不需要在这儿处理 exchange binding的 queue
        channel.exchangeDeclare("ex.routing", BuiltinExchangeType.DIRECT, true, false, null);

        for (int i = 0; i < 100; i++) {
            String level = LOG_LEVEL[RANDOM.nextInt(100) % LOG_LEVEL.length];
            channel.basicPublish("ex.routing", level, null, (i+":这是"+level+"级别的消息").getBytes(StandardCharsets.UTF_8));
        }

        channel.close();
        connection.close();
    }
}
